package capricornus.components.scrollbar
{
	import capricornus.components.sprite.AbstractComponent;
	
	import fl.controls.UIScrollBar;
	import fl.events.ScrollEvent;
	
	[Event(name="change", type="capricornus.components.scrollbar.ScrollBarEvent")]
	public class ScrollBar extends AbstractComponent
	{
		public static const HORIZONTAL:int = 0;
		public static const VERITICAL:int = 1;
		
		private var _direct:int = 1;

		private var _uiScrollBar:UIScrollBar;
		
		public function ScrollBar()
		{
			super();
			init();
		}
		
		private function init():void
		{
			_uiScrollBar = new UIScrollBar();
			_uiScrollBar.addEventListener( ScrollEvent.SCROLL, onScroll, false, 0, true );
			addChild(_uiScrollBar);
		}
		
		public function destroy():void
		{
			_uiScrollBar.removeEventListener( ScrollEvent.SCROLL, onScroll );
			_uiScrollBar = null;
		}
		
		/**
		 * 宽高都设置不了0 
		 * @param w
		 * @param h
		 * 
		 */		
		override public function setSize( w:int, h:int ):void
		{
			if( _direct==HORIZONTAL )
			{
				if( h != 0 ) _uiScrollBar.width = h;
				if( w != 0 ) _uiScrollBar.height = w;
			}
			else
			{
				if( w != 0 ) _uiScrollBar.width = w;
				if( h != 0 ) _uiScrollBar.height = h;
			}
		}
		
		override public function get width():Number
		{
			if( _direct==HORIZONTAL ) return _uiScrollBar.height;
			return _uiScrollBar.width;
		}
		
		override public function get height():Number
		{
			if( _direct==HORIZONTAL ) return _uiScrollBar.width;
			return _uiScrollBar.height;
		}
		
		public function get maxScrollSize():int
		{
			return _uiScrollBar.maxScrollPosition;
		}
		
		public function setViewInfo( viewPortLen:int, maxLen:int ):void
		{
			_uiScrollBar.pageSize = viewPortLen;
			_uiScrollBar.maxScrollPosition = maxLen - viewPortLen;
		}
		
		public function set direction( value:int ):void
		{
			if( _direct != value )
			{
				_direct = value;
				if( _direct==HORIZONTAL )
				{
					_uiScrollBar.rotation = -90;
					_uiScrollBar.y = _uiScrollBar.width;
				}
				else
				{
					_uiScrollBar.rotation = 0;
					_uiScrollBar.y = 0;
				}
			}
		}
		
		public function set scrollPosition( value:Number ):void
		{
			_uiScrollBar.scrollPosition = value;
			dispatchEvent( new ScrollBarEvent( ScrollBarEvent.CHANGE, _uiScrollBar.scrollPosition/_uiScrollBar.maxScrollPosition, 0 ) );
		}
		
		public function get scrollPosition():Number
		{
			return _uiScrollBar.scrollPosition;
		}
		
		public function set proportion( value:Number ):void
		{
			_uiScrollBar.scrollPosition = value * _uiScrollBar.maxScrollPosition;
			dispatchEvent( new ScrollBarEvent( ScrollBarEvent.CHANGE, _uiScrollBar.scrollPosition/_uiScrollBar.maxScrollPosition, 0 ) );
		}
		
		private function onScroll( e:ScrollEvent ):void
		{
			dispatchEvent( new ScrollBarEvent( ScrollBarEvent.CHANGE, _uiScrollBar.scrollPosition/_uiScrollBar.maxScrollPosition, e.delta ) );
		}
	}
}


















